<#include "../../display.ftl"/>
<#assign shmsConstant = statics["cn.hfatec.shms.constants.ShmsConstant"]/>
<!DOCTYPE html>
<html>
<head>
    <script style="text/javascript">
        $(function () {
            $(".checkTable").change(function () {
                var flag = true;
                if ($(this).prop("checked")) {
                    $(this).parent().parent().children("td").children("input[type=checkbox]").attr("checked", true);

                    $(this).parent().parent().children("td:not(:first)").children("input[type=checkbox]").parent("td").attr("bgcolor", "#e8e8e8");

                    if ($(this).parent().parent("tr").attr("class") != undefined && $(this).parent().parent("tr").attr("class") == "assembly open") {
                        $(this).parent().parent().nextAll("tr").each(function () {
                            var childrenTr = $(this).attr("class");
                            if (flag) {
                                if (childrenTr != undefined && childrenTr != "assembly open") {
                                    $(this).children("td").children("input[type=checkbox]").attr("checked", true);

                                    $(this).children("td").children("input[type=checkbox]").parent("td").attr("bgcolor", "#e8e8e8")
                                } else {
                                    flag = false;
                                }
                            }
                        });
                    }
                } else {
                    $(this).parent().parent().children("td").children("input[type=checkbox]").attr("checked", false);
                    $(this).parent().parent().children("td:not(:first)").removeAttr("bgcolor");

                    if ($(this).parent().parent("tr").attr("class") != undefined && $(this).parent().parent("tr").attr("class") == "assembly open") {
                        $(this).parent().parent().nextAll("tr").each(function () {
                            var childrenTr = $(this).attr("class");
                            if (flag) {
                                if (childrenTr != undefined && childrenTr != "assembly open") {
                                    $(this).children("td").children("input[type=checkbox]").attr("checked", false);
                                    $(this).children("td").removeAttr("bgcolor")
                                } else {
                                    flag = false;
                                }
                            }
                        });
                    }
                }
            });

            $("input[name=authId]").change(function () {
                if (!$(this).prop("checked")) {
                    $(this).attr("checked", false);
                } else {
                    $(this).attr("checked", true);
                }
            });

            $("#checkAll").change(function () {
                if ($(this).prop("checked")) {
                    $("#assign_module_auth_table input[name=authId]").attr("checked", true);
                    $("#assign_module_auth_table input.checkTable").attr("checked", true);
                    $("#assign_module_auth_table input[name=authId]").parent("td").attr("bgcolor", "#e8e8e8");
                } else {
                    $("#assign_module_auth_table input[name=authId]").attr("checked", false);
                    $("#assign_module_auth_table input.checkTable").attr("checked", false);
                    $("#assign_module_auth_table input[name=authId]").parent("td").removeAttr("bgcolor");
                }
            });


            $(".show-close").click(function (event) {
                treeClick($(this).parent().parent("tr"));
            });


        });


        function treeClick(tr) {
            if (tr.hasClass("open")) {
                tr.removeClass("open");
                tr.find(".element").removeClass("show-tree");
            }
            else {
                tr.addClass("assembly open");
            }
            var next = tr.next();
            while ($(next).attr("class").indexOf("element") != -1) {
                $(next).toggleClass("show-tree");
                next = $(next).next();
            }
        }

        function save() {
            var authIds = new Array();
            $("#search_assign_authdata_table").find("table tbody tr").find("input[name=authId]:checked").each(function () {
                authIds.push($(this).val());
            });

            $("#" + "search_assign_authdata_table").ajaxSubmit({
                url: '${absoluteContextPath}/system/role/assignAuth',
                data: {'authIds': authIds},
                success: function (data) {
                    data = JSON.parse(data);
                    if (data.returnCode == 1) {
                        $.showMessage({parentId: "showMessage", message: "分配权限成功！", type: "success", mode: "modal"});
                    } else {
                        $.showMessage({parentId: "showMessage", message: "<strong>错误信息</strong>：" + data.returnMsg, type: "success", mode: "modal"});
                    }
                }
            });
        }
    </script>
</head>
<body>
<div class="breadcrumbs">
    <ol class="breadcrumb">
        <li><a href="${absoluteContextPath}/home"><i class="fa fa-home"></i>首页</a></li>
        <li><a href="#">用户管理</a></li>
        <li><a href="#">角色分配权限</a></li>

    </ol>
</div>

<div class="main">


    <section class="tile">
        <div id="showMessage"><#include "../../message.ftl" /></div>
        <div class="tile-body nopadding">
            <div class="table-responsive">
                <form class="form-horizontal" role="form" id="search_assign_authdata_table" method="post"
                      action="${absoluteContextPath}/system/role/goAssignAuth">
                    <input id="roleId" type="hidden" name="roleId" value="${roleId!''}"/>
                    <table class="tree-table table table-datatable table-gray">
                        <thead class="title-table">
                        <tr>
                            <th>模块</th>
                            <th>权限</th>
                        </tr>
                        </thead>
                        <tbody id="assign_module_auth_table">
                        <#if modulesTable??>
                            <#list modulesTable as module>
                            <tr class="assembly open">
                                <td style="width: 25%">
                                    <#if module.childrenModuleList?? && module.childrenModuleList?size gt 0>
                                        <em class="show-close"></em>
                                    </#if>
                                ${module.moduleName!''}
                                    <input class="checkTable" type="checkbox"/><label
                                        for="opt01"></label>&nbsp;
                                </td>
                                <td>
                                    <#if module.authDataList??>
                                        <#list module.authDataList as authData>
                                            <#if assignAuthList?seq_contains(authData.authId!'')>
                                                <input name="authId" type="checkbox" value="${authData.authId}"
                                                       checked="checked"/><label
                                                    for="opt01">${authData.authName}</label>&nbsp;
                                            <#else>
                                                <input name="authId" type="checkbox" value="${authData.authId}"/><label
                                                    for="opt01">${authData.authName}</label>&nbsp;
                                            </#if>
                                        </#list>
                                    </#if>
                                </td>
                            </tr>

                                <#if module.childrenModuleList??>
                                    <#assign modulesTable=module.childrenModuleList/>
                                    <#include "childrenModuleAuth.ftl"/>
                                </#if>
                            </#list>
                        </#if>
                        <tr>
                            <td>全选<input id="checkAll" class="checkTable" type="checkbox"/><label
                                    for="opt01"></label>&nbsp;</td>
                            <td>
                                <button type="button" class="btn btn-default" onclick="window.close()">关闭</button>
                                <button type="button" class="btn btn-primary" onclick="save();">保存</button>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </form>
            </div>
        </div>
    </section>
</div>
</body>
